package com.xjrsoft.module.customer.erp_model.eRP_Turnover.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.Func;
import com.xjrsoft.module.base.service.IXjrBaseCodeRuleService;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.dto.*;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.entity.ErpStockinventory;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.entity.ErpStockinventoryentry;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.service.IErpStockinventoryService;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.vo.ErpStockinventoryFormDataVo;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.vo.ErpStockinventoryListVo;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.vo.ErpStockinventoryVo;
import com.xjrsoft.module.customer.erp_model.eRP_Turnover.vo.ErpStockinventoryentryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  控制器
 *
 * @author pp
 * @since 2021-11-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("/erp_turnover")
@Api(value = "盘盈审核", tags = "盘盈审核接口")
public class ErpStockinventoryController {


	private final IErpStockinventoryService erpStockinventoryService;

	private final IXjrBaseCodeRuleService codeRuleService;

	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入erpStockinventory")
	public Response<ErpStockinventoryFormDataVo> getFormData(@PathVariable String id) {
		ErpStockinventoryFormDataVo formDataVo = new ErpStockinventoryFormDataVo();
		// 主表数据
		ErpStockinventory erpStockinventory = erpStockinventoryService.getById(id);
		formDataVo.setErpStockinventoryVo(BeanUtil.copy(erpStockinventory, ErpStockinventoryVo.class));
		// 从表数据
		formDataVo.setErpStockinventoryentryVoList(BeanUtil.copyList(erpStockinventoryService.getErpStockinventoryentryByParentId(id), ErpStockinventoryentryVo.class));
		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入erpStockinventory")
	public Response<PageOutput<ErpStockinventoryListVo>> getPageList(ErpStockinventoryListDto listDto) {
		IPage<ErpStockinventoryListVo> page = erpStockinventoryService.getListInfo(listDto);
		List<ErpStockinventoryListVo> records = BeanUtil.copyList(page.getRecords(), ErpStockinventoryListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		// 工作流数据处理
		//FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增
	 */
//	@PostMapping
//	@ApiOperation(value = "新增", notes = "传入erpStockinventory")
//	public Response save(@RequestBody SaveErpStockinventoryFormDataDto formDto) {
//		ErpStockinventoryDto erpStockinventoryDto = formDto.getErpStockinventoryDto();
//		ErpStockinventory erpStockinventory = BeanUtil.copy(erpStockinventoryDto, ErpStockinventory.class);
//		List<ErpStockinventoryentryDto> erpStockinventoryentryDtoList = formDto.getErpStockinventoryentryDto();
//		List<ErpStockinventoryentry> erpStockinventoryentryList = BeanUtil.copyList(erpStockinventoryentryDtoList, ErpStockinventoryentry.class);
//		String dataId = erpStockinventoryService.addErpStockinventory(erpStockinventory, erpStockinventoryentryList);
//		if (dataId != null) {
//			codeRuleService.useEncode("InStockCode");
//		}
//		return dataId == null ? Response.notOk() : Response.ok(dataId);
//	}
	@PostMapping("/updateInventory")
	@ApiOperation(value = "审核", notes = "传入erpStockinventory")
	public Response updateInventory(@RequestBody UpdateInventorDtoList updateInventoryDto)
	{
		List<UpdateInventoryDto> updateInventoryDtos = updateInventoryDto.getUpdateInventoryDtos();
		return Response.ok(erpStockinventoryService.updateInventory(updateInventoryDtos));
	}


	/**
	 * 修改 
	 */
//	@PutMapping("/{id}")
//	@ApiOperation(value = "修改", notes = "传入erpStockinventory")
//	public Response update(@PathVariable String id, @RequestBody UpdateInventorDtoList formDto) {
//		ErpStockinventoryDto erpStockinventoryDto = formDto.getErpStockinventoryDto();
//		ErpStockinventory erpStockinventory = BeanUtil.copy(erpStockinventoryDto, ErpStockinventory.class);
//
//		List<ErpStockinventoryentryDto> erpStockinventoryentryDtoList = formDto.getErpStockinventoryentryDto();
//		List<ErpStockinventoryentry> erpStockinventoryentryList = BeanUtil.copyList(erpStockinventoryentryDtoList, ErpStockinventoryentry.class);
//		return Response.status(erpStockinventoryService.updateErpStockinventory(id, erpStockinventory, erpStockinventoryentryList));
//	}


	
	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(erpStockinventoryService.removeByIds(Func.toStrList(ids)));
	}

	
}
